﻿using System;
using System.ComponentModel;
using Notoric.Gui.Windows;

namespace Notoric.Gui.Commands
{
    /// <summary>
    /// Implements a command that inserts a new text section into an active page.
    /// </summary>
    public class InsertTextSectionCommand : RibbonCommand
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="InsertTextSectionCommand"/> instance.
        /// </summary>
        /// <param name="mainWindow">
        /// <para>
        /// Type: <see cref="MainWindow"/>
        /// </para>
        /// <para>
        /// Reference to the application's main window.
        /// </para>
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// The <paramref name="mainWindow"/> is <see langword="null"/>.
        /// </exception>
        public InsertTextSectionCommand(MainWindow mainWindow)
            : base(mainWindow)
        {
            ScreenTipTitle = "Insert a Text Section";
            ScreenTipText = "Creates a new text section in the current page.";
            DisableReason = "A page must be open first.";

            mainWindow.PropertyChanged += new PropertyChangedEventHandler(mainWindow_PropertyChanged);
        }

        #endregion

        #region ICommand Members

        /// <inheritdoc/>
        public override bool CanExecute(object parameter)
        {
            return ApplicationWindow.ActivePage != null;
        }

        /// <inheritdoc/>
        public override void Execute(object parameter)
        {
            var cmd = new ViewModel.NewTextSectionCommand(ApplicationWindow.ActivePage.UndoStack, ApplicationWindow.ActivePage.RedoStack)
            {
                Owner = ApplicationWindow.ActivePage
            };

            var appState = ((App)App.Current).CurrentState;
            appState.MouseTarget = typeof(Controls.Page);
            appState.Command = cmd;
            appState.StatusBarMessage = "Draw the area where you want to put the section.";
        }

        #endregion

        #region Event Handlers

        /// <summary>
        /// Occurs when a property of the main application window has changed.
        /// </summary>
        /// <param name="sender">Object that has sent the event</param>
        /// <param name="e">Event data</param>
        private void mainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ActivePage")
                RaiseCanExecuteChanged();
        }

        #endregion
    }
}
